<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Follow;
use app\common\model\Customer as CustomerModel;
use app\common\model\User as UserModel;
use think\Db;
/**
 * 
 * 首页接口
 */
class Manage extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function getFollowList()
    {
        // 如果是 Selectpage 请求
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        // 获取分页参数
        $page = $this->request->post('page', 1);
        $pageSize = $this->request->post('pageSize', 20);

        // 查询条件
        $where = [];
        $searchKey = $this->request->post('searchKey', '');
        if ($searchKey) {
            $where['customer_name|follow_type|remark'] = ['like', "%{$searchKey}%"];
        }

        $follow = new \app\common\model\Follow();
        $total = $follow->where($where)->count();
        $list = $follow
            ->where($where)
            ->order('id', 'desc')
            ->page($page, $pageSize)
            ->select();

        $this->success('请求成功', ['list' => $list, 'total' => $total]);
    }


    public function getCustomerList()
    {
        // 如果是 Selectpage 请求
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        // 获取分页参数
        $page = $this->request->post('page', 1);
        $pageSize = $this->request->post('pageSize', 20);


        // 查询条件
        $where = [];
        $searchKey = $this->request->post('searchKey', '');
        if ($searchKey) {
            $where['customerName|phone'] = ['like', "%{$searchKey}%"];
        }
        $where['is_delete'] = 0;
        $where['is_claim'] = 1;
        $customer = new \app\common\model\Customer();
        $total = $customer->where($where)->count();
        $list = $customer
            ->where($where)
            ->order('id', 'desc')
            ->page($page, $pageSize)
            ->select();

        $this->success('请求成功', ['list' => $list, 'total' => $total]);
    }


    public function getContractsList()
    {
        // 如果是 Selectpage 请求
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        // 获取分页参数
        $page = $this->request->post('page', 1);
        $pageSize = $this->request->post('pageSize', 20);
        $customerId = $this->request->post('customerId', 'customerId');

        // 查询条件
        $where = [];
        $searchKey = $this->request->post('searchKey', '');
        if ($searchKey) {
            $where['name|customer_name|phone|manager'] = ['like', "%{$searchKey}%"];
        }

        if(!empty($customerId)){
            $where['customer_id'] = $customerId;
        }

        $contracts = new \app\common\model\Contracts();
        $total = $contracts->where($where)->count();
        $list = $contracts
            ->where($where)
            ->order('id', 'desc')
            ->page($page, $pageSize)
            ->select();

        $this->success('请求成功', ['list' => $list, 'total' => $total]);
    }


    public function getOperationLogList()
    {
        // 如果是 Selectpage 请求
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        // 获取分页参数
        $page = $this->request->post('page', 1);
        $pageSize = $this->request->post('pageSize', 20);
        $customerId = $this->request->post('customerId', 'customerId');

        // 查询条件
        $where = [];
        if(!empty($customerId)){
            $where['customer_id'] = $customerId;
        }

        $total =Db::name('operation_log l')
            ->join('user u', 'l.user_id = u.id')
            ->where($where)
            ->count();

        $list = Db::name('operation_log l')
            ->join('user u', 'l.user_id = u.id')
            ->field('l.*,u.name')
            ->where($where)
            ->order('id', 'desc')
            ->page($page, $pageSize)
            ->select();

        $this->success('请求成功', ['list' => $list, 'total' => $total]);
    }



}
